//
//  CommuViewController.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/6.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  交流区

import UIKit
import WebKit


class CommuViewController : WKBaseViewController,WKBaseDelegate {
    
    
    ///调试模式
    fileprivate lazy var debugLabel : UILabel = {
        let lab = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: SW, height: 24))
        lab.layer.borderColor = UIColor.black.cgColor
        lab.sizeToFit()
        lab.numberOfLines = 0
        lab.font = UIFont.systemFont(ofSize: 12)
        return lab
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.wkDelegate = self
        
        
        //背景色--白色
        view.backgroundColor = UIColor.white
        
        self.navigationItem.title = "交流区"
        
        if commuArray.count > 1 {
            let urlStr = commuArray.lastObject as! String
            
            webView.load(URLRequest.init(url: URL.init(string: urlStr)!))
            
        } else {
            
            DispatchQueue.main.async {
                
                let urlStr = jlqURL + "?devtype=1&token=" + self.token
                
                //判断是否包含域名
                if urlStr.contains(comStrURL) {
                    let url = URL.init(string: urlStr)
                    
                    
                    //根据是否有网络状态加载断网图片
                    StatusBarCheck.shared.networkingStatesFromStatebar { (result) in
                        
                        if result == "notReachable" {
                            
                            DispatchQueue.main.async {
                                
                                self.urlRequestCache = NSURLRequest.init(url: url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
                                self.webView.load(self.urlRequestCache as URLRequest)
                            }
                        } else {
                            DispatchQueue.main.async {
                                
                                self.urlRequestCache = NSURLRequest(url: url!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10)
                                self.webView.load(self.urlRequestCache as URLRequest)
                                
                            }
                        }
                    }
                } else {
                    CustomAlertView.shared.alertWithTitle(strTitle: "请下拉刷新")
                }
                
            }
        }
        
    }

    //拦截操作
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        url = (navigationAction.request.url?.absoluteString)!
        
        //域名鉴别
        if navigationAction.navigationType == WKNavigationType.linkActivated || url == "http://\(yuMing)/app/message_list.aspx" || url == "http://\(yuMing)/app/message.aspx" {
            
            
            if url.contains("?") {
                url = url + ("&devtype=1&token=") + (token)
            } else {
                url = url + ("?devtype=1&token=") + (token)
            }
            
            //相同url处理
            if fwqArray.count > 0 && (fwqArray.lastObject as! String) == self.url {
                self.webView.reload()
                decisionHandler(.allow)
                
                self.view.isUserInteractionEnabled = false
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                    self.view.isUserInteractionEnabled = true
                })
                
                fwqArray.removeLastObject()
                return
            }
            
            commuArray.add(url)
            aaa(str: self.url)
            
            //XFLog(message: url)
            
            //取消拦截
            decisionHandler(.cancel)
            
            
        } else {
            //允许拦截
            decisionHandler(.allow)
        }
        
    }
    
    //url---为false不跳，还原设置
    func aaa(str : String) -> Void {
        
        
        let vvv = CommuViewReplace()
        vvv.url = str
        self.hidesBottomBarWhenPushed = true
        
        
        push(vc: vvv, flag: true)
        
        self.hidesBottomBarWhenPushed = false
    }
    
    //只允许当前类使用该方法
    private func push(vc : UIViewController,flag : Bool) -> Void {
        
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
}


// MARK:- 实现代理方法
extension CommuViewController {
    func server() {
        
        self.navigationController!.popToRootViewController(animated: true)
        
        webView.load(URLRequest.init(url: URL.init(string: jlqURL)!))
        
    }
    
}

